How to Use Two Github Accounts on Mac

Pre-requisite:

my two github user.name is miqianmimi & miqianmimi7
my two github user.email is miqainmimi@outlook.com & miqianmimi7@outlook.com

First: generate two keys and four documents.

1
2
ssh-keygen -t rsa -C "miqainmimi@outlook.com"  
ssh-keygen -t rsa -C "miqianmimi7@outlook.com"

when enter name , you may use

1
2
id_rsa_one
id_rsa_two

when enter password , you may use space (set none password)

After this first part, you have 4 documents

1
2
3
4
1. id_rsa.one
2. id_rsa.one.pub
3. id_rsa.two
4. id_rsa.two.pub

Second: add public and private keys.

1
2
3
ssh-agent -s
ssh-add ~/.ssh/id_rsa_one
ssh-add ~/.ssh/id_rsa_two

After this first part, you have already added the Public key and Private key


Third: rewrite your config

1
touch config

This is the config content

1
2
3
4
5
6
7
8
9
10
11
#miqianmimi account
Host github.com-one
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_one

#miqianmimi7 account
Host github.com-two
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_two

After this part, you have finished a config document.


Forth: deploy SSH Key

Go into the your two github accounts and than go to Personal settings –> SSH and GPG keys

copy the key From

1
2
id_rsa.one.pub
id_rsa.two.pub

To your two github keys.

After this part, you have related your two github accounts with your two SSH keys.


Fifth: test whether they are linked

1
2
ssh –T github.com-one
ssh -T github.com-two

When it is success: it will show that : the key on the github change from to

After this part, There is only one thing left.


Sixth: Clone you repo and modify your Git config

use first account : clone your repo and change git config like

1
2
3
git clone git@github.com-one:miqianmimi/pingmesh-graduate-project-2018.git
git config user.name "miqianmimi"
git config user.email "miqainmimi@outlook.com"

Then, the git add . & git commit a & git push are all right.


use second account : clone your repo and change git config like

1
2
3
git clone git@github.com-two:miqianmimi7/so-high.git
git config user.name "miqianmimi7"
git config user.email "miqainmimi7@outlook.com"

Then, the git add . & git commit a & git push are all right.

Reference :

CSDN Blogs
GitHubGist